import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/login'
import axios from '@/utils/api'
Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      meta:{
        title: '登录'
      },
      component: Login
    },
    {
      path: '/register',
      name: 'register',
      meta: {
        title: '注册'
      },
      component: () => import('./views/register')
    },
    {
      path: '/',
      name: 'layout',
      redirect: { name: 'news' },
      component: () => import('./views/layout/index'),
      children: [
        {
          path: 'news',
          name: 'news',
          meta: {
            title: '消息',
            icon: 'plus'
          },
          component: () => import('./views/layout/news'),
        },{
          path: 'contacts',
          name: 'contacts',
          meta: {
            title: '联系人',
            icon: 'sun',
          },
          component: () => import('./views/layout/contacts'),
        },{
          path: '/indoor',
          name: 'indoor',
          meta: {
            title: '广场',
            icon: 'bullhorn',
          },
          component: () => import('./views/layout/indoor/index')
        },{
          path: '/my',
          name: 'my',
          meta: {
            title: '个人主页',
            icon: 'sun',
          },
          component: () => import('./views/layout/my/index')
        }
      ]
    },
    {
      path: '/video',
      name: 'video',
      meta: {
        title: '风采'
      },
      component: () => import('./views/video/index')
    }
  ]
})
router.beforeEach((to, from, next) => {
  const { name } = to;
  if(name == 'login' || name == 'register'){
    next();
  } else {
    axios.post('/user/verification')
      // .then( ({ data })=> data.success ? next() : next('/login') )
  }
  next();
})
export default router

